package com.cpi.cornpp.exchangerateversion.service;

import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.exchangerateversion.converter.ExchangeRateVersionConverter;
import com.cpi.cornpp.exchangerateversion.dao.ExchangeRateVersionDAO;
import com.cpi.cornpp.exchangerateversion.dto.ExchangeRateVersionDTO;
import com.cpi.cornpp.exchangerateversion.exception.ExchangeRateVersionException;
import com.cpi.cornpp.exchangerateversion.view.ExchangeRateGroupView;
import com.cpi.cornpp.exchangerateversion.view.ExchangeRateVersionView;

public class ExchangeRateVersionService {

	/**
	 * 
	 * @param versionId
	 * @return
	 * @throws ExchangeRateVersionException
	 */
	public List getExchangeRateVersion(int versionId)throws ExchangeRateVersionException{
		List exchangeRateVersionViews = null;
		List exchangeRateVersionDTOs = null;
		ExchangeRateVersionDAO exchangeRateVersionDAO = null;
		ExchangeRateVersionConverter exchangeRateVersionConverter = null;
		try {
			exchangeRateVersionDAO = new ExchangeRateVersionDAO();
			exchangeRateVersionConverter = new ExchangeRateVersionConverter();
			exchangeRateVersionDTOs = exchangeRateVersionDAO.getExchangeRateVersionById(versionId);
			exchangeRateVersionViews = exchangeRateVersionConverter.convertDTOsToViews(exchangeRateVersionDTOs);
		}catch (ExchangeRateVersionException exchangeRateVersionException) {			
			throw exchangeRateVersionException;
		}catch (Exception exception){
			ExchangeRateVersionException exchangeRateVersionException=null;
			exception.printStackTrace(System.out);
			exchangeRateVersionException= new ExchangeRateVersionException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw exchangeRateVersionException;
		}
		return exchangeRateVersionViews;
	}
	
	/**
	 * Obtiene grupos de currencys (agrupados por currency)
	 * @param versionId
	 * @return
	 * @throws ExchangeRateVersionException
	 */
	public List getGroups(int versionId) throws ExchangeRateVersionException{
		ExchangeRateVersionConverter converter = null;
		converter = new ExchangeRateVersionConverter();
		List views = getExchangeRateVersion(versionId);
		return converter.viewGroups(views) ;
	}
	
	/**
	 * Actualiza los grupos de exchange rates (agrupados por currency)
	 * @param groups
	 * @throws ExchangeRateVersionException
	 */
	public void updateExchangeRate(List groups)throws ExchangeRateVersionException{
		ExchangeRateVersionConverter converter = null;
		ExchangeRateVersionDTO dto = null;
		ExchangeRateVersionDAO exchangeRateVersionDAO = null;
		try {			
			converter = new ExchangeRateVersionConverter();
			exchangeRateVersionDAO = new  ExchangeRateVersionDAO();
			for (Iterator iter = groups.iterator(); iter.hasNext();) {
				ExchangeRateGroupView group = (ExchangeRateGroupView) iter.next();
				for(Iterator iter2 = group.getExchangeRateVersionViews().iterator(); iter2.hasNext();){
					ExchangeRateVersionView view = (ExchangeRateVersionView)iter2.next();
					dto = converter.convertViewToDTO(view);
					exchangeRateVersionDAO.update(dto);
				}
			}
		} catch (ExchangeRateVersionException exchangeRateVersionException) {	
			throw exchangeRateVersionException;
		}catch (Exception exception){
			ExchangeRateVersionException exchangeRateVersionException=null;
			exception.printStackTrace(System.out);
			exchangeRateVersionException= new ExchangeRateVersionException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw exchangeRateVersionException;
		}
	}
}